﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebLibSolPedBE;
using SAP.Middleware.Connector;
using LibSolPedI;

namespace LibSolPedDALC
{
    public class SolicitudCajaChicaDALC : ISolCajaChica
    {
        private SolicitudCajaChicaDALC() { }
        private static volatile SolicitudCajaChicaDALC _instance = null;

        public static SolicitudCajaChicaDALC Instance()
        {
            if (_instance == null)
            {
                lock (typeof(SolicitudCajaChicaDALC))
                {
                    if (_instance == null)
                    {
                        _instance = new SolicitudCajaChicaDALC();
                    }
                }
            }
            return _instance;
        }

        public List<SolCajaChicaBE> getLstSolCajaChica()
        {
            List<SolCajaChicaBE> lst = new List<SolCajaChicaBE>();

            IRfcFunction function = null;
            RfcDestination dest = null;
            DestConfig dc = new DestConfig();
            try
            {
                function = DALC.rfcRep.CreateFunction("Z_MOVIL_FI_GET_REPOAPRO_CC_CAB");

                function.Invoke(DALC.rfcDest);
                IRfcTable T_RETORNO = function.GetTable("EP_TRETURN");
                foreach (IRfcStructure item in T_RETORNO)
                {
                    SolCajaChicaBE it = new SolCajaChicaBE();
                    it.BELNR = item["BELNR"].GetString();
                    it.KUNNR = item["KUNNR"].GetString();
                    it.WAERS = item["WAERS"].GetString();
                    it.WRBTR = item["WRBTR"].GetString();
                    it.BUDAT = item["BUDAT"].GetString();
                    it.BUKRS = item["BUKRS"].GetString();
                    it.GJAHR = item["GJAHR"].GetString();
                    it.LIFNR = item["LIFNR"].GetString();
                    it.DLIFNR = item["DLIFNR"].GetString();
                    lst.Add(it);
                }
            }
            catch (Exception e)
            {

            }

            return lst;
        }

        public List<SolCajaChicaDetBE> getLstSolCajaChicaDet(string IP_NROREP, string IP_CAJA, string IP_BUKRS, string IP_GJAHR)
        {
            List<SolCajaChicaDetBE> lst = new List<SolCajaChicaDetBE>();

            IRfcFunction function = null;
            RfcDestination dest = null;
            DestConfig dc = new DestConfig();
            try
            {
                function = DALC.rfcRep.CreateFunction("Z_MOVIL_FI_GET_REPOAPRO_CC_DET");
                function.SetValue("IP_NROREP", IP_NROREP);
                function.SetValue("IP_CAJA", IP_CAJA);
                function.SetValue("IP_BUKRS", IP_BUKRS);
                function.SetValue("IP_GJAHR", IP_GJAHR);

                function.Invoke(DALC.rfcDest);
                IRfcTable T_RETORNO = function.GetTable("EP_TRETURN");
                foreach (IRfcStructure item in T_RETORNO)
                {
                    SolCajaChicaDetBE it = new SolCajaChicaDetBE();

                    it.DOCLIQ = item["DOCLIQ"].GetString();
                    it.BUDAT = item["BUDAT"].GetString();
                    it.BLART = item["BLART"].GetString();
                    it.DBLART = item["DBLART"].GetString();
                    it.LIFNR = item["LIFNR"].GetString();
                    it.DLIFNR = item["DLIFNR"].GetString();
                    it.KOSTL = item["KOSTL"].GetString();
                    it.DKOSTL = item["DKOSTL"].GetString();
                    it.GASTO = item["GASTO"].GetString();
                    it.DGASTO = item["DGASTO"].GetString();
                    it.WAERS = item["WAERS"].GetString();
                    it.WRBTR = item["WRBTR"].GetString();
                    lst.Add(it);
                }
            }
            catch (Exception e)
            {

            }

            return lst;
        }

        public string setAprobarSolCajaChica(List<SolCajaChicaBE> lst)
        {
            String ret = "Resultado de libetación: <br>" + Environment.NewLine;
            try
            {
                foreach (SolCajaChicaBE it in lst)
                {
                    IRfcFunction function = null;
                    function = DALC.rfcRep.CreateFunction("Z_MOVIL_FI_APROB_ANTICIPO_CC");
                    function.SetValue("IP_BELNR", it.BELNR);
                    function.SetValue("IP_BUKRS", it.BUKRS);
                    function.SetValue("IP_GJAHR", it.GJAHR);
                    function.Invoke(DALC.rfcDest);
                    IRfcTable T_RETORNO = function.GetTable("TR_RETORNO");
                    foreach (IRfcStructure item in T_RETORNO)
                    {
                        ret = ret + "  -" + item["MESSAGE"].GetString() + ". <br>";
                    }
                    ret = ret + "<br>" + Environment.NewLine;
                }
            }
            catch (Exception ex)
            {
                ret = ret + "<br>" + "Error en el resto de documentos";
            }
            return ret;
        }
    }
}
